﻿using System.Collections.Generic;
using System.Linq;
using compshop.Modules.GeneralProducts.Views;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;
using System.Windows;


namespace compshop.Modules.GeneralProducts.Presenter
{
    public class GeneralProductsPresenter
    {
        private readonly CompShopEntities contextDS;
        private EventsManager eventsManager;
        private string stateShell;

        private IList<GeneralProduct> generalProductList;
        private IList<Country> countryList;
        private IList<Category> categoryList;
        private IList<Producer> producerList;
        private IList<Parameter> parameterList;

        private int selectedCategoryID;
        private int selectedSubCategoryID;
        private int selectedProducerID;

        public IViewGeneralProducts View { get; set; }

        public IList<GeneralProduct> GeneralProductList
        {
            get { return generalProductList; }
            set { generalProductList = value; }
        }

        public IList<Country> CountryList
        {
            get { return countryList; }
            set { countryList = value; }
        }

        public IList<Category> CategoryList
        {
            get { return categoryList; }
            set { categoryList = value; }
        }

        public IList<Producer> ProducerList
        {
            get { return producerList; }
            set { producerList = value; }
        }

        public IList<Parameter> ParameterList
        {
            get { return parameterList; }
            set { parameterList = value; }
        }

        public int SelectedProducerID
        {
            get { return selectedProducerID; }
            set
            {
                selectedProducerID = value;
                LoadGeneralProducts();
                View.Presenter = this;
            }
        }

        public string StateShell 
        {
            get
            {
                return stateShell;
            }
            set 
            {
                stateShell = value;
            }
        }

        public GeneralProductsPresenter(EventsManager eventsManager)
        {
            contextDS = CompShopEntitiesContext.GetInstance();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();

            selectedProducerID = 0;
            selectedSubCategoryID = 0;
            selectedCategoryID = 0;

            generalProductList = new List<GeneralProduct>();
            if (generalProductList == null)
                generalProductList = new List<GeneralProduct>();

            countryList = contextDS.Countries.ToList<Country>();
            if (countryList == null)
                countryList = new List<Country>();

            categoryList = contextDS.Categories.ToList<Category>();
            if (categoryList == null)
                categoryList = new List<Category>();

            parameterList = contextDS.Parameters.ToList<Parameter>();
            if (parameterList == null)
                parameterList = new List<Parameter>();

            producerList = contextDS.Producers.ToList<Producer>();
            if (producerList == null)
                producerList = new List<Producer>();

            Producer pr = new Producer();
            pr.Name = "All";
            pr.ProducerID = 0;
            producerList.Insert(0, pr);
        }


        private void LoadGeneralProducts()
        {
            //if (selectedProducerID == 0 && selectedSubCategoryID == 0 && selectedCategoryID == 0)
            //    generalProductList = contextDS.GeneralProducts.Expand("Country,Producer,SubCategory").ToList<GeneralProduct>();


            if (selectedSubCategoryID != 0)
            {
                if (selectedProducerID != 0)
                    generalProductList = contextDS.GeneralProducts.Expand("Country,Producer,SubCategory").
                        Where(x => x.SubCategoryID == selectedSubCategoryID & x.ProducerID == selectedProducerID).ToList<GeneralProduct>();
                else
                    generalProductList = contextDS.GeneralProducts.Expand("Country,Producer,SubCategory").
                        Where(x => x.SubCategoryID == selectedSubCategoryID).ToList<GeneralProduct>();
            }

            if (selectedCategoryID != 0)
            {
                if (selectedProducerID != 0)
                    generalProductList = contextDS.GeneralProducts.Expand("Country,Producer,SubCategory").
                        Where(x => x.SubCategory.CategoryID == selectedCategoryID & x.ProducerID == selectedProducerID).ToList<GeneralProduct>();
                else
                    generalProductList = contextDS.GeneralProducts.Expand("Country,Producer,SubCategory").
                        Where(x => x.SubCategory.CategoryID == selectedCategoryID).ToList<GeneralProduct>();
            }

            foreach (GeneralProduct gp in generalProductList)
            {
                contextDS.LoadProperty(gp, "Country");
                contextDS.LoadProperty(gp, "Producer");
                contextDS.LoadProperty(gp, "SubCategory");
                contextDS.LoadProperty(gp.SubCategory, "Category");
            }

        }

        public void ShowCreateGeneralProductDialog()
        {
            GeneralProduct genProduct = new GeneralProduct();
            CreateGeneralProductDialog productDialog = new CreateGeneralProductDialog(genProduct);
            productDialog.DataContext = this;
            if (productDialog.ShowDialog() == true)
            {
                contextDS.AddToGeneralProducts(genProduct);
                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
                else
                {
                    LoadGeneralProducts();
                    View.Presenter = this;
                }
            }
        }


        public void ShowEditGeneralProductDialog(GeneralProduct product)
        {
            CreateGeneralProductDialog productDialog = new CreateGeneralProductDialog(product);
            productDialog.DataContext = this;
            if (productDialog.ShowDialog() == true)
            {
                contextDS.UpdateObject(product);
                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
                else
                {
                    LoadGeneralProducts();
                    View.Presenter = this;
                }
            }
        }


        #region Publishers

        public void GeneralProductSelectedPublisher(int id)
        {
            eventsManager.EventAggregator.GetEvent<GeneralProductSelectedEvent>().Publish(id);
        }

        public bool AddToBasketPublisher(int id)
        {
            Product prod = contextDS.Products.
                Where(x => x.GeneralProductID == id & x.ClientOrderID == null & x.Status == 1).FirstOrDefault();

            if (prod != null)
            {
                eventsManager.EventAggregator.GetEvent<AddToBasketEvent>().Publish((int)prod.ProductID);
                return true;
            }
            else
                return false;
        }

        #endregion


        #region Handlers

        public void CategorySelectedEventHandler(int id)
        {
            selectedSubCategoryID = 0;
            selectedCategoryID = id;
            LoadGeneralProducts();
            View.Presenter = this;
        }

        public void SubCategorySelectedEventHandler(int id)
        {
            selectedSubCategoryID = id;
            selectedCategoryID = 0;
            LoadGeneralProducts();
            View.Presenter = this;
        }

        public void ProducerSelectedEventHandler(int id)
        {
            SelectedProducerID = id;
        }

        public void GeneralProductSelectedEventHandler(int id)
        {

        }

        #endregion

    }   //public class GeneralProductsPresenter
}









//GeneralProduct genProduct = new GeneralProduct();
//genProduct.Name = productDialog.tboxName.Text;

//Producer prd=(Producer)productDialog.cboxProducer.SelectedItem;
//genProduct.ProducerID =prd.ProducerID;
//prd.GeneralProducts.Add(genProduct);

//SubCategory scat=(SubCategory)productDialog.cboxSubCategory.SelectedItem;
//genProduct.SubCategoryID = scat.SubCategoryID;
//scat.GeneralProducts.Add(genProduct);

//Country country=(Country)productDialog.cboxCountry.SelectedItem;
//genProduct.CountryID = country.CountryID;
//country.GeneralProducts.Add(genProduct);

//if (!string.IsNullOrEmpty(productDialog.tboxBarcode.Text))
//{
//    genProduct.BarCode = productDialog.tboxBarcode.Text;
//}

//if (!string.IsNullOrEmpty(productDialog.tboxInfo.Text))
//{
//    genProduct.BarCode = productDialog.tboxInfo.Text;
//}

//if (!string.IsNullOrEmpty(productDialog.tboxPrice.Text))
//{
//    genProduct.CurrentPrice =Convert.ToDecimal(productDialog.tboxPrice.Text);
//}

//if (productDialog.imagePhoto.Source != null)
//{
//    //string path = ((BitmapImage)productDialog.imagePhoto.Source).UriSource.AbsoluteUri;
//    string path = (string)productDialog.imagePhoto.Tag;
//    FileStream fs=File.OpenRead(path);
//    genProduct.Photo=new byte[fs.Length];
//    fs.Read(genProduct.Photo, 0, (int)fs.Length);                  
//    fs.Close();
//}